-- Width and height of map in tiles
mapW = 0
mapH = 0

-- With and height of each tile in pixels
tileW = 100
tileH = 100

-- Width and height of map in pixels
local mapWpixels
local mapHpixels

-- Some arrays to store images and tiles
local maptile = {}
local map = {}

-- The size of the map in tiles
local rows = 6
local columns = 96

-- Store globally which physics bodies belong to the world
bodiesOfMap = {}

function addRectangleCollision(i, j, x, y, sx, sy, mass)
	bodies[i] = love.physics.newBody(world, x+(sx/2), y+(sy/2), mass, 0)
	shapes[i] = love.physics.newRectangleShape(bodies[i], 0, 0, sx, sy, 0)
	bodiesOfMap[j] = i
end

-- Initializing tiles
function initMap()
	-- Temp storage
	temp = 1
	-- Let's loop through every tile in the map
	-- to add the images to our array. If the tile
	-- doesn't exist (which I made sure that they didn't by
	-- deleting all black tiles) it just adds a empty value.
	for i = 1, columns do
		for j = 1, rows do
			mapFileNameTemp = "Content/groundmap/Tile_C"..i.."_R"..j..".png"
			if(love.filesystem.exists( mapFileNameTemp )) then
				maptile[temp] = love.graphics.newImage( mapFileNameTemp )
			else
				maptile[temp] = ""
			end
			temp = temp+1
		end
	end
	
	-- Now lets set up the tile map it self. Make it 0 if there is
	-- no tile to show.
	temp = 1
	for i = 1, columns do
		map[i] = {}
		for j = 1, rows do
			if (maptile[temp] == "") then
				map[i][j] = 0
			else
				map[i][j] = temp
			end
			temp = temp + 1
		end
	end
	
	-- Define height and width in tiles
	mapW = #map
	mapH = #map[1]

	-- Set up physics for map
	addRectangleCollision(0, 0, 0, 522, 592, 78, 0)
	addRectangleCollision(2, 1, 580, 240, 310, 32, 0)
	addRectangleCollision(3, 2, 905, 522, 1545, 78, 0)
	addRectangleCollision(4, 3, 2600, 522, 4268, 78, 0)
	addRectangleCollision(5, 4, 3330, 235, 35, 290, 0)
	x = 2100
	y = 347.5
	bodies[6] = love.physics.newBody(world, x, y, 0, 0)
	shapes[6] = love.physics.newPolygonShape(bodies[6], 1860-x, 500-y, 2010-x, 210-y, 2030-x, 195-y, 2320-x, 188-y, 2345-x, 253-y, 2028-x, 263-y, 1905-x, 500-y, 1860-x, 500-y)
	bodiesOfMap[5] = 6
	
	x = 1915+(550/2)
	y = 275+(247/2)
	bodies[7] = love.physics.newBody(world, x, y, 0, 0)
	shapes[7] = love.physics.newPolygonShape(bodies[7], -275, 123.5, -265, 108.5, -232, 86.5, -223, 33.5, -199, 10.5, -188, -56.5, 150, 112.5, 169, -120.5, 275, 104.5, 270, 123.5)
	bodiesOfMap[6] = 7
	
	addRectangleCollision(8, 7, 700+(80/2), 200+(40/2), 80, 40, 500)
	
	bodies[9] = love.physics.newBody( world, 560, 390, 1 )
	shapes[9] = love.physics.newCircleShape( bodies[9], 0, 0, 20 )
	
	end

-- Jolly good function to round a number to idp precision
function round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

-- Just some updating
function updateMap(dt)
	if  playerX > screenW + cameraX - 300 and cameraX < (tileW * columns) then
		cameraX = cameraX + 4
	end
	if playerX < 300 + cameraX and cameraX > 0 then
		cameraX = cameraX - 4
	end
end

-- Drawing
function drawMap()
	-- Lets loop through every tile to draw it
	-- making sure not to draw tiles that are outside
	-- our viewport.
	for x=1, mapW do
        for y=1, mapH do
			if((map[x][y] ~= 0)) then
				love.graphics.draw(
					maptile[map[x][y]],
					(x-1) * tileW,
					(y-1) * tileH)
			end
        end
    end
	
	love.graphics.circle("fill", bodies[9]:getX(), bodies[9]:getY(), shapes[9]:getRadius(), 20)
end